﻿Imports System.Data.Linq
Public Class CBangKeChungTuController
    Dim objDataContext As QLyNganSachDataContext
    Public Function SelectRecordByID(ByVal _id As Integer) As tblBangKeChungTu
        Try
            Return (From r In objDataContext.tblBangKeChungTus
                    Where r.ID_BangKeChungTu = _id
                    Select r).Single
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
    Public Function SelectRecordByCondition(ByVal _idDonVi As Integer, ByVal _idDanhMuc As Integer, ByVal _idMaLoaiNganSach As Integer, ByVal iThang As Integer, ByVal iNam As Integer) As tblBangKeChungTu
        Try
            Return (From r In objDataContext.tblBangKeChungTus
                    Where r.Id_DonVi = _idDonVi And r.ID_DanhMuc = _idDanhMuc And r.ID_MaLoaiNganSach = _idMaLoaiNganSach And r.ThangQuyetToan = iThang And r.NamQuyetToan = iNam
                    Select r).Single
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
    Public Function InsertRecord(ByVal LocalTable As tblBangKeChungTu) As Integer
        Dim id As Integer = -1
        Try
            objDataContext.tblBangKeChungTUs.InsertOnSubmit(LocalTable)
            objDataContext.SubmitChanges()
            id = LocalTable.ID_BangKeChungTu
        Catch ex As Exception
            Throw ex
        End Try
        Return id
    End Function
    Public Sub UpdateRecord(ByVal LocalTable As tblBangKeChungTu)
        Try
            objDataContext.tblBangKeChungTUs.Attach(LocalTable)
            objDataContext.Refresh(RefreshMode.KeepCurrentValues, LocalTable)
            objDataContext.SubmitChanges(ConflictMode.ContinueOnConflict)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
    Public Sub UpdateRecordWithoutAttach(ByVal LocalTable As tblBangKeChungTu)
        Try
            objDataContext.Refresh(RefreshMode.KeepCurrentValues, LocalTable)
            objDataContext.SubmitChanges(ConflictMode.ContinueOnConflict)
        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Public Sub DeleteRecord(ByVal _id As Integer)
        Try
            objDataContext.tblBangKeChungTus.DeleteOnSubmit( _
              (From r In objDataContext.tblBangKeChungTus _
               Where r.ID_BangKeChungTu = _id).Single)
            objDataContext.SubmitChanges()
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
   Public Sub New()
        objDataContext = New QLyNganSachDataContext
    End Sub
    Public Sub New(ByVal _obj As QLyNganSachDataContext)
        objDataContext = _obj
    End Sub
End Class
